package com.lightkits.wms.domain.mainpage.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.CommonMethodsUtil;
import com.lightkits.common.db.entity.FndOrganizationBase;
import com.lightkits.common.db.entity.FndOrganizationTl;
import com.lightkits.common.model.CurrentWorkerInfo;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.database.dao.WmsWarehouseMapper;
import com.lightkits.wms.database.model.FndWarehouseModel;
import com.lightkits.wms.domain.mainpage.service.WareHouseInfoDomainService;
import com.lightkits.wms.dto.mainpage.model.WareHouseInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * WareHouseInfoDomainServiceImpl
 *
 * @author menxiao.wu@hand-china.com
 * Created on 2019-02-21 20:07
 */
@Service
public class WareHouseInfoDomainServiceImpl implements WareHouseInfoDomainService {
    private final WmsWarehouseMapper mapper;

    @Autowired
    public WareHouseInfoDomainServiceImpl(WmsWarehouseMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public List<WareHouseInfoDTO> listWareHouseInfo(String warehouseCode,
                                                    String organizationId,
                                                    String categroySet,
                                                    String wmAreaFlag) {
        List<FndWarehouseModel> fndWarehouseModels = new ArrayList<>();
        try {
            //获取默认组织
            if(ObjectUtil.isNullOrEmpty(organizationId)){
                CurrentWorkerInfo currentWorkerInfo = CommonMethodsUtil.getCurrentWorkerInfo(null);

                FndOrganizationBase base = new FndOrganizationBase().selectOne(
                        new EntityWrapper()
                                .eq(FndOrganizationTl.ORGANIZATION_ID, currentWorkerInfo.getDefaultOrganizationId()));
                if (ObjectUtil.isEmpty(base)) {
                    return new ArrayList<>();
                }
                organizationId=base.getOrganizationId().toString();
            }
            //仓库查询
            fndWarehouseModels = mapper.queryFndWarehouse(warehouseCode,
                    organizationId,
                    categroySet,
                    wmAreaFlag);
        } catch (Exception e) {
            throw new LightKitsBizException("Query The Warehouse Error,Maybe the return more");
        }
        List<WareHouseInfoDTO> wareHouseInfoDTOS = fndWarehouseModels.stream().map(
                t -> {
                    WareHouseInfoDTO dto = new WareHouseInfoDTO();
                    dto.setWarehouseId(ObjectUtil.toString(t.getWarehouseId()));
                    dto.setWarehouseCode(t.getWarehouseCode());
                    dto.setDescription(t.getDescription());
                    dto.setWmAreaFlag(t.getWmAreaFlag());
                    dto.setNegativeFlag(t.getNegativeFlag());
//                    FndCategoryBase categoryBase=new FndCategoryBase().selectById(dto.getWmPendingAraeId())
                    dto.setWmPendingAraeId(t.getWmPendAraeId());
                    dto.setWmStockAreaId(t.getWmStockAraeId());
                    return dto;
                }
        ).collect(Collectors.toList());
        return wareHouseInfoDTOS;
    }
}
